<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>列表渲染</title>
</head>

<body>
    <div id="app">
        <h2>测试：v-for遍历数组</h2>
        <ul>
            <li v-for="(p,index) in persons" :key="index">{{index}}--{{p.name}}--{{p.age}}
                --<button @click="deleteP(index)">删除</button>
                --<button @click="updateP(index,{name:'djsj',age:20})">更新</button>
            </li>
        </ul>
        <h2>测试：v-for遍历对象</h2>
        <ul>
            <li v-for="(value,key) in persons[1]" :key="key">{{value}}--{{key}}</li>
        </ul>
    </div>
    <script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                persons: [{
                        name: 'Tom1',
                        age: 18
                    },
                    {
                        name: 'Tom2',
                        age: 13
                    },
                    {
                        name: 'Tom3',
                        age: 15
                    },
                    {
                        name: 'Tom4',
                        age: 19
                    }
                ]
            },
            methods: {
                deleteP(index) {
                    //删除persons中指定index的p
                    this.persons.splice(index, 1)
                },
                updateP(index, newP) {
                    //this.persons[index] = newP;//并没有改变persons本身，而是改变了数组内部的结构，并没有调用变异方法，vue不会更新
                    this.persons.splice(index, 1, newP)
                },
            }
        })
    </script>
</body>

</html>